/**
 * 版权所有 2009-2015 旺晓杨 peonyxy@msn.cn
 **/
package com.cn;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.ac.cintcm.sh.domain.HerbEntry;
import cn.ac.cintcm.sh.service.HerbService;

public class TraditionalServlet extends HttpServlet {

	/**
     * 
     */
    private static final long serialVersionUID = -39571242770796644L;


    /**
	 * Constructor of the object.
	 */
	public TraditionalServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setCharacterEncoding("UTF-8");
		String doClass = request.getParameter("doClass");
		if(doClass != null && doClass.equals("query"))
		{
			this.query(request, response);
		}
	}
	
	
	/**
	 * ��ˎ��ԃ
	 * @param request
	 * @param response
	 */
	public void query(HttpServletRequest request, HttpServletResponse response){
		String tradName = request.getParameter("tradName");
		 PrintWriter out = null;
		 StringBuffer str = new StringBuffer("");
			
			try {
				HerbService herbService = new HerbService();
				if(tradName == null)
					tradName = "";
				Iterator iterator = herbService.getHerbs(tradName, "false", "false").iterator();
				str.append("{articles:[");
				if(iterator.hasNext())
				{
					HerbEntry herbEntry = (HerbEntry)iterator.next();
					str.append("{'id':"+herbEntry.getId()+",'text':'"+herbEntry.getName()+"'}");
			
					while(iterator.hasNext()){
						herbEntry = (HerbEntry)iterator.next();
						str.append(",{'id':"+herbEntry.getId()+",'text':'"+herbEntry.getName()+"'}");
					}
				}
				str.append("]}");
				 out = response.getWriter();
			} catch (Exception e) {
				str.append("{articles:[{'id':'','text':'<font color=red>��ԃ����!</font>'}]}");
			}
		 out.print(str);
		 out.flush();
	}
	

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}